package main

import (
	"html/template"
	"log"
	"net/http"
	"net/url"
)

type Controller struct {
	Rw   http.ResponseWriter
	Form url.Values
	Data map[interface{}]interface{}
	Tpl  string
}

type ControllerInterface interface {
	Init(rw http.ResponseWriter, form url.Values)
	Get()
	Post()
}

func (c *Controller) Init(rw http.ResponseWriter, form url.Values) {
	c.Rw = rw
	c.Form = form
	http.Error(c.Rw, "Method Not Allowed", 405)
}

func (c *Controller) Get() {
	http.Error(c.Rw, "Method Not Allowed", 405)
}

func (c *Controller) Post() {
	http.Error(c.Rw, "Method Not Allowed", 405)
}

func (c *Controller) Display(data interface{}) {
	file := Viewpath + c.Tpl
	log.Printf("加载file:%s\n", file)
	t := template.New("funs")
	t = t.Funcs(template.FuncMap{"time2": U2t})
	t, err := t.ParseFiles(Viewpath+"header.tpl", file, Viewpath+"footer.tpl")
	checkErr(err)

	// err = t.Execute(rw, data)
	err = t.ExecuteTemplate(c.Rw, "header", data)
	err = t.ExecuteTemplate(c.Rw, "content", data)
	err = t.ExecuteTemplate(c.Rw, "footer", nil)
	checkErr(err)
}
